package com.tiantian.quanminxiu.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.LoginPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.view.LoginView;
import com.tiantian.quanminxiu.wxapi.WXEntryActivity;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/18.
 */
public class Activity_LoginSelect extends ActivityBase implements LoginView{

    private LoginPresent present;

    private String url = "http://www.quanminxiu.tv/agreement/user";
    private View imgLoginWeixin,imgLoginQQ,imgLoginXiu;
    private TextView txtLoginClause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loginselect);

        Init();

        InitView();
        present = new LoginPresent();
        present.attachView(this);

    }

    @Override
    public void InitView() {
        imgLoginWeixin = findViewById(R.id.imgLoginWeixin);
        imgLoginQQ = findViewById(R.id.imgLoginQQ);
        imgLoginXiu = findViewById(R.id.imgLoginXiu);

        txtLoginClause = (TextView) findViewById(R.id.txtLoginClause);

        RxView.clicks(imgLoginQQ).map(View->imgLoginQQ).subscribe(OC_Button);
        RxView.clicks(imgLoginWeixin).map(View->imgLoginWeixin).subscribe(OC_Button);
        RxView.clicks(imgLoginXiu).map(View->imgLoginXiu).subscribe(OC_Button);
        RxView.clicks(txtLoginClause).map(View->txtLoginClause).subscribe(OC_Button);
    }

    public Action1<View> OC_Button = v->{
        Loading_Open(true);
        switch (v.getId()){
            case R.id.imgLoginQQ:
                Loading_Close();
                CommonUtils.showToask(thisContext,getResources().getString(R.string.not_yet_completed));
//                JumpLoginQQ();
                break;
            case R.id.imgLoginWeixin:
               JumpLoginWeixin();
                break;
            case R.id.imgLoginXiu:
                APPUtils.Login_To(thisActivity);
                finish();
                break;
            case R.id.txtLoginClause:
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(url);
                intent.setData(content_url);
                thisContext.startActivity(intent);
                Loading_Close();
                break;
        }
    };


    @Override
    public void PageInit() {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    public void JumpLoginQQ() {
        APPUtils.QQHelperLogin_To(this);
        Activity_QQLoginHelper.setQQLoginHelperBackCall(new Activity_QQLoginHelper.QQLoginHelperBackCall() {
            @Override
            public void backcall(String openid, String access_token, String expires_in, String refresh_token, String nickname, String sex) {
                present.loginQQ(openid,access_token,expires_in,refresh_token,nickname,sex);
            }
            @Override
            public void onError(String msg) {
                Activity_LoginSelect.this.Loading_Close();
            }
        });
    }

    @Override
    public void JumpLoginWeixin() {
        WXEntryActivity.startWXlogin(thisContext, new WXEntryActivity.WXEntryBackCall() {
            @Override
            public void backcall(String openid, String unionid, String access_token, String expires_in, String refresh_token, String nickname, String sex) {
                present.loginWeixin(openid, unionid, access_token,
                        expires_in, refresh_token, nickname, sex);
            }
            @Override
            public void onError(String msg) {
                Activity_LoginSelect.this.Loading_Close();
            }
        });
    }

    @Override
    public void LoginAccount() {
    }

    @Override
    public void JumpMain() {
        APPUtils.Main_To(thisActivity);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        present.detachView();
    }
}
